package com.bidding.common.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class GlobalConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(mappingJackson2HttpMessageConverter());
    }

    private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        ObjectMapper objectMapper = new ObjectMapper();
        // 自定义 ObjectMapper 配置,如果说实体出现为空，可以正常返回不会报错
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        // 将所有返回前端的属性为null的值舍除掉
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper);
        return converter;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("Sa-Token拦截器已注册");
        // 注册 Sa-Token 拦截器
        registry.addInterceptor(new SaInterceptor(handle -> {
                    // 指定一些路径无需权限验证
                    SaRouter.match("/api/**")
                            .notMatch("/api/auth/login") // 登录接口
                            .notMatch("/api/auth/register") // 注册接口
                            .notMatch("/api/auth/captcha") // 验证码接口
                            .notMatch("/api/doc.html") // Knife4j接口文档
                            .notMatch("/api/webjars/**") // Knife4j静态资源
                            .notMatch("/api/swagger-resources/**") // Swagger资源
                            .notMatch("/api/v2/api-docs") // API文档
                            .notMatch("/api/favicon.ico") // 网站图标
                            .notMatch("/api/error") // 错误页面
                            .check(r -> StpUtil.checkLogin()); // 检查登录状态

                    // 针对不同角色的权限验证
                    SaRouter.match("/api/system/user/**", r -> StpUtil.checkRole("admin"));
                    // 如果还有其他角色权限路径，可以继续添加
                }))
                .addPathPatterns("/**"); // 拦截所有请求，依靠 SaRouter 进行细粒度控制
    }
}